// Copyright (c) 2019 Presto Labs Pte. Ltd.
// Author: taekwon

#include "coin2/exchange/util/rate_limit.h"

#include <gtest/gtest.h>

TEST(RateLimiter, constructor) {
  auto limiter_1 = RateLimiter(5, 1.0, 0);
  EXPECT_TRUE(limiter_1.CheckAndSetWithTimestamp(10000));
  EXPECT_TRUE(limiter_1.CheckAndSetWithTimestamp(20000));
  EXPECT_TRUE(limiter_1.CheckAndSetWithTimestamp(30000));
  EXPECT_TRUE(limiter_1.CheckAndSetWithTimestamp(40000));
  EXPECT_TRUE(limiter_1.CheckAndSetWithTimestamp(50000));
  EXPECT_FALSE(limiter_1.CheckAndSetWithTimestamp(60000));

  auto limiter_2 = RateLimiter(5, 1.0, 0);
  EXPECT_TRUE(limiter_2.CheckAndSetWithTimestamp(10000));
  EXPECT_TRUE(limiter_2.CheckAndSetWithTimestamp(20000));
  EXPECT_TRUE(limiter_2.CheckAndSetWithTimestamp(30000));
  EXPECT_TRUE(limiter_2.CheckAndSetWithTimestamp(40000));
  EXPECT_TRUE(limiter_2.CheckOnlyWithTimestamp(50000));
  EXPECT_TRUE(limiter_2.CheckAndSetWithTimestamp(60000));
}

TEST(QtyLimiter, constructor) {
  auto limiter_1 = QtyLimiter(5, 1.0, 0);
  EXPECT_TRUE(limiter_1.CheckAndSetWithQtyTimestamp(4, 10000));
  EXPECT_TRUE(limiter_1.CheckAndSetWithQtyTimestamp(1, 20000));
  EXPECT_FALSE(limiter_1.CheckAndSetWithQtyTimestamp(1, 30000));

  auto limiter_2 = QtyLimiter(5, 1.0, 0);
  EXPECT_TRUE(limiter_2.CheckAndSetWithQtyTimestamp(4, 10000));
  EXPECT_TRUE(limiter_2.CheckOnlyWithQtyTimestamp(1, 20000));
  EXPECT_TRUE(limiter_2.CheckAndSetWithQtyTimestamp(1, 30000));
}

TEST(WindowLimiter, constructor) {
  auto limiter = WindowLimiter(5, 10);
  EXPECT_TRUE(limiter.CheckAndSetWithTimestamp(1));
  EXPECT_TRUE(limiter.CheckAndSetWithTimestamp(2));
  EXPECT_TRUE(limiter.CheckAndSetWithTimestamp(3));
  EXPECT_TRUE(limiter.CheckAndSetWithTimestamp(4));
  EXPECT_TRUE(limiter.CheckAndSetWithTimestamp(5));
  EXPECT_FALSE(limiter.CheckAndSetWithTimestamp(6));
  EXPECT_FALSE(limiter.CheckAndSetWithTimestamp(8));
  EXPECT_TRUE(limiter.CheckAndSetWithTimestamp(11));
  EXPECT_TRUE(limiter.CheckAndSetWithTimestamp(12));
}
